<?php
    
    
    /**
     * 页面json 输出
     *
     * @param int $code
     * @param     $msg
     * @param     $paras
     * $code = 0 请求正常
     * $code = 1 请求失败
     * $code = 2 其他情况
     */
    function responseToJson($code = 0, $msg = '', $paras = null)
    {
        $res["code"] = $code;
        $res["msg"] = $msg;
        // if (!empty($paras)) {
        $res["result"] = $paras;
        // }
        return response()->json($res)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
    }
    
    
    /**
     * 判断字符串是否在所需字符串首
     *
     * @param string $str
     * @param string $needle
     */
    function startWith($str, $needle)
    {
        return strpos($str, $needle) === 0;
    }
    
    
    /**
     * 判断字符串是否在所需字符串尾
     */
    function endWith($hayStack, $needle)
    {
        $length = strlen($needle);
        if ($length == 0) {
            return true;
        }
        return (substr($hayStack, -$length) === $needle);
    }
    
    
    /**
     * 获取用户密码加密字符串
     *
     * @param $password
     * @param $salt
     *
     * @return string
     */
    function encryptPassword($passWord)
    {
        return md5(md5($passWord));
    }
    
    
    /**
     * 返回一串float数字
     */
    function millisecond()
    {
        return floor(microtime(true) * 1000);
    }
    
    /**
     * 得到记录的时间距离当前时间的长度
     *
     * @param  string $big 现在时间
     * @param  string $little 记录更新时间
     */
    function timeDiff($big, $little)
    {
        $diff = $big - $little;
        if (0 <= $diff && $diff < 60) {
            return $diff . "秒前";
        } elseif (60 <= $diff && $diff < 3600)
            return (floor($diff / 60)) . "分钟前";
        elseif (3600 <= $diff && $diff < 86400)
            return (floor($diff / 3600)) . "小时前";
        elseif (86400 <= $diff && $diff < 2592000)
            return (floor($diff / 86400)) . "天前";
        elseif (2592000 <= $diff && $diff < 62208000)
            return (floor($diff / 43200 * 2592000)) . "月前";
        elseif (62208000 <= $diff)
            return (floor($diff / 518400 * 60)) . "年前";
    }
    
    
    /*
     * 生成n位随机字符串
     * str 字符串
     * num 位数
     * */
    function strRand($num = 10, $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
    {
        $key = '';
        for ($i = 0; $i < $num; $i++) {
            $key .= $str[mt_rand(0, strlen($str) - 1)];
        }
        return $key;
    }
    
    
    function generateCode($length = 6)
    {
        return rand(pow(10, ($length - 1)), pow(10, $length) - 1);
    }
    
    /*
     * 把秒转换成天数，小时数，分钟
     * $secs 秒
     * */
    function secsToStr($secs)
    {
        $r = '';
        if ($secs >= 86400) {
            $days = floor($secs / 86400);
            $secs = $secs % 86400;
            $r = $days . ' 天 ';
        }
        if ($secs >= 3600) {
            $hours = floor($secs / 3600);
            $secs = $secs % 3600;
            
            $r .= $hours . ' 小时 ';
        }
        if ($secs >= 60) {
            $minutes = floor($secs / 60);
            $secs = $secs % 60;
            $r .= $minutes . ' 分钟 ';
        }
        return $r;
    }
    
    
    /**
     * 字符串和数组的转换
     * $val  需要转换的数组或者字符串
     * $explStr  规定在哪里分割字符串或合并数组
     */
    function strChangeArr($val, $explStr = '')
    {
        
        $value = null;
        is_string($val) ? $value = explode($explStr, trim($val)) : (is_array($val) ? $value = implode($explStr, $val) : $value = 0);
        return $value;
    }
    
    
    /** 获得session中用户的id
     * @return int|string
     */
    function get_session_user_id()
    {
        $user = session("user");
        
        return $user ? $user->id : 0;
    }
    
    
    /**
     * 字节转ＭＢ
     */
    function getByteToMb($bytes)
    {
        $mb = $bytes / 1048576;
        return round($mb, 0);
    }
    
    /**
     * 将数字字符串数组转换为数字数组
     *
     * @param $val 带转换的字符串数字数组
     */
    function changeStringToInt($val)
    {
        if ($val == '' || $val == null)
            return [];
        for ($i = 0; $i < sizeof($val); $i++) {
            $val[$i] = intval($val[$i]);
        }
        return $val;
    }
    
    /**
     * 删除数组中指定的值
     */
    function deleteArrValue($arr, $val)
    {
        array_splice($arr, array_search($val, $arr), 1);
        
        return $arr;
    }
    
    
    function mergeRepeatArray(...$arrays)
    {
        if (count($arrays) == 1)
            return array_unique($arrays);
        else {
            $array_b = [];
            foreach ($arrays as $key => $array)
                $array_b += array_flip($array);
            return array_keys($array_b);
            
        }
    }
    
    /**
     * 字符串超出部分以指定字符串代替
     *
     */
    function changeString($str = '', $start = 0, $length, $replace = '...', $codeType = 'utf-8')
    {
        return mb_strlen($str, $codeType) < ($start + $length) ? $str : mb_substr($str, $start, $length, $codeType) . $replace;
    }
    
    
    /**
     * 登录成功存储用户会话状态
     */
